<template>
  <el-container class="home-container">
    <!--头部-->
    <el-header>
      <div>
        <img src="../../assets/img/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!--主体-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
            :default-active="activeIndex"
            class="el-menu-vertical-demo"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409EFF"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            >
          <!--一级菜单-->
          <el-submenu v-for="(menuItem, index) in menuList" :key="menuItem.id" :index="menuItem.id + ''">
            <template slot="title">
              <!--图标-->
              <i :class="['iconfont', iconList[index]]"></i>
              <!--文本-->
              <span>{{ menuItem['authName'] }}</span>
            </template>
            <!--二级菜单-->
            <el-menu-item v-for="subItem in menuItem.children" :key="subItem.id" :index="'/' + subItem.path"
            @click="saveActiveIndex('/' + subItem.path)">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-menu"></i>
                <!--文本-->
                <span>{{ subItem['authName'] }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--右侧主题-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import {menuRequest} from "../../network/menu";

export default {
  name: "Home",
  created() {
    this.getMenuList()
  },
  data() {
    return {
      menuList: [],
      iconList: ['icon-user', 'icon-tijikongjian', 'icon-showpassword',
        'icon-danju', 'icon-danju'],
      isCollapse: false,
    }
  },
  computed: {
    activeIndex() {
      return window.sessionStorage.getItem('activeIndex')
    }
  },
  methods: {
    // 退出登录
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 请求菜单
    getMenuList() {
      menuRequest().then(res => {
        // console.log(res)
        this.menuList = res.data
        // console.log(this.menuList[0])
      }).catch(err => {
        this.$message.error('请求菜单失败')
        console.log(err)
      })
    },
    // 控制菜单是否收起
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存点击的Index
    saveActiveIndex(activeIndex) {
      // 为了网站刷新时也有效就保存到sessionStorage里面
      window.sessionStorage.setItem('activeIndex', activeIndex)
    }
  }
}
</script>

<style lang="less" scoped>

.home-container {
  height: 100%;
}

.el-header {
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  background-color: #373d41;

  > div {
    display: flex;
    align-items: center;
  }

  span {
    margin-left: 15px;
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: none;
  }
}

.el-container {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  // 设置间隔
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>